package com.quanyan.alipay.protocal;

import com.quanyan.alipay.config.AlipayConfig;
import com.quanyan.alipay.sign.SignUtils;
import com.quanyan.common.utils.TimeUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;


/**
 * Created by zijun.song on 2015/9/14.
 */
public class AliPayReqData {

    private String service = "mobile.securitypay.pay"; //接口名称。固定值。

    private String partner;//签约的支付宝账号对应的支付宝唯一用户号。以2088开头的16位纯数字组成。

    private String _input_charset;//商户网站使用的编码格式，固定为utf-8。

    private String sign_type;

    private String sign; //参数签名.

    private String notify_url;

    ///////////////////////////////////////////////////////////////////

    private String out_trade_no; //支付宝合作商户网站唯一订单号。

    private String subject; //商品名称

    private String body; //商品详情

    private String payment_type = "1";

    private String seller_id;

    private BigDecimal total_fee; //总金额. 精确到小数点后两位. 0.01

    private String it_b_pay; //超时时间 2014-06-13 16:00:00

    private AlipayConfig alipayConfig;

//    private String app_id; //标示客户端.
//    private String appenv; //客户端来源.appenv=”system=android^version=3.0.1.2”
//    private String extern_token; //授权令牌


    public AliPayReqData(String out_trade_no, String subject, String body, BigDecimal total_fee, Integer payTimeOut, AlipayConfig alipayConfig) {
        // it_b_pay
        if (payTimeOut == null || payTimeOut < 0) {
            payTimeOut = 30;//分钟
        }
        Date d = TimeUtils.getDateAfterMinutes(new Date(), payTimeOut);
        this.it_b_pay = TimeUtils.dateToEngString(d, TimeUtils.DF_PATTERN_YYYY_MM_DD_HH_MM_SS);

        this.alipayConfig = alipayConfig;
        this.partner = alipayConfig.getPartner();
        this._input_charset = alipayConfig.getInput_charset();
        this.sign_type = alipayConfig.getSign_type();
        this.notify_url = alipayConfig.getNotify_url();
        this.seller_id = alipayConfig.getSeller();

        this.out_trade_no = out_trade_no;
        this.subject = subject;
        this.body = body;
        this.total_fee = total_fee;
    }

    public String sign() {
        try {
            StringBuffer buffer = new StringBuffer();
            buffer.append("partner=\"" + this.partner + "\"");
            buffer.append("&seller_id=\"" + this.seller_id + "\"");
            buffer.append("&out_trade_no=\"" + this.out_trade_no + "\"");
            buffer.append("&subject=\"" + this.subject + "\"");
            buffer.append("&body=\"" + this.body + "\"");
            buffer.append("&total_fee=\"" + this.total_fee + "\"");
            buffer.append("&notify_url=\"" + this.notify_url + "\"");
            buffer.append("&service=\"" + this.service + "\"");
            buffer.append("&payment_type=\"" + this.payment_type + "\"");
            buffer.append("&_input_charset=\"" + this._input_charset + "\"");
            buffer.append("&it_b_pay=\"" + this.it_b_pay + "\"");

            System.out.println(buffer.toString());
            String sign = SignUtils.sign2(buffer.toString(), alipayConfig.getPrivate_key());
            buffer.append("&sign=\"" + URLEncoder.encode(sign, alipayConfig.getInput_charset()) + "\"");
            buffer.append("&sign_type=\"" + alipayConfig.getSign_type() + "\"");
            return buffer.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }


    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getPartner() {
        return partner;
    }

    public void setPartner(String partner) {
        this.partner = partner;
    }

    public String get_input_charset() {
        return _input_charset;
    }

    public void set_input_charset(String _input_charset) {
        this._input_charset = _input_charset;
    }

    public String getSign_type() {
        return sign_type;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getPayment_type() {
        return payment_type;
    }

    public void setPayment_type(String payment_type) {
        this.payment_type = payment_type;
    }

    public String getSeller_id() {
        return seller_id;
    }

    public void setSeller_id(String seller_id) {
        this.seller_id = seller_id;
    }

    public BigDecimal getTotal_fee() {
        return total_fee;
    }

    public void setTotal_fee(BigDecimal total_fee) {
        this.total_fee = total_fee;
    }

    public String getIt_b_pay() {
        return it_b_pay;
    }

    public void setIt_b_pay(String it_b_pay) {
        this.it_b_pay = it_b_pay;
    }

}
